package com.yyt.elasticsearch.dao.impl;

import java.util.List;
import java.util.Arrays;

import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;

public class ProductDaoImpl {

    @Autowired
    private RestHighLevelClient client;

    public String searchProduct(String prod_keywords, List<String[]> item) {
//        SearchRequest searchRequest = new SearchRequest();
//        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//        searchSourceBuilder.query(QueryBuilders.matchAllQuery()); // 添加 match_all 查询
//        searchRequest.source(searchSourceBuilder); // 将 SearchSourceBuilder  添加到 SeachRequest 中

        // prod_keywords 的模糊匹配
        SearchRequest searchRequest = new SearchRequest("product");  // 设置搜索的 index
//        searchRequest.types("doc");  // 设置搜索的 type

        MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("prod_keywords", prod_keywords);
        matchQueryBuilder.fuzziness(Fuzziness.AUTO);
        matchQueryBuilder.prefixLength(3);
        matchQueryBuilder.maxExpansions(10);

        // item 的精准匹配
        int size = item.size();

        // 精准匹配产品类型
        String[] prod_type_array = item.get(0);
        int prod_type_size = prod_type_array.length;
        for (int j=0; j<prod_type_size; j++) {
            String prod_type = prod_type_array[j];

            matchQueryBuilder.fuzziness();  // 精准查询
            matchQueryBuilder.prefixLength(3); // 前缀查询的长度
            matchQueryBuilder.maxExpansions(10); // max expansion 选项，用来控制模糊查询
            QueryBuilder matchQueryBuilderProdType = QueryBuilders.matchQuery("category_name", prod_type)
                    .fuzziness();
        }

        // 精准匹配产品名称
        String[] prod_name_array = item.get(1);
        int prod_name_size = prod_name_array.length;
        for (int j=0; j<prod_name_size; j++) {
            String prod_name = prod_name_array[j];

            matchQueryBuilder.fuzziness();  // 精准查询
            matchQueryBuilder.prefixLength(3); // 前缀查询的长度
            matchQueryBuilder.maxExpansions(10); // max expansion 选项，用来控制模糊查询
            QueryBuilder matchQueryBuilderProdName = QueryBuilders.matchQuery("prod_name", prod_name)
                    .fuzziness();
//                    .fuzziness(Fuzziness.AUTO)
//                    .prefixLength(3)
//                    .maxExpansions(10);
        }

        // 精准匹配热门推荐
        String[] prod_hot_array = item.get(2);
        int prod_hot_size = prod_hot_array.length;
        for (int j=0; j<prod_hot_size; j++) {
            String prod_hot = prod_hot_array[j];

            matchQueryBuilder.fuzziness();  // 精准查询
            matchQueryBuilder.prefixLength(3); // 前缀查询的长度
            matchQueryBuilder.maxExpansions(10); // max expansion 选项，用来控制模糊查询
            QueryBuilder matchQueryBuilderProdHot = QueryBuilders.matchQuery("prod_name", prod_hot)
                    .fuzziness();
        }

        // 精准匹配起始港
        String[] prod_sports_array = item.get(3);
        int prod_sports_size = prod_sports_array.length;
        for (int j=0; j<prod_sports_size; j++) {
            String prod_sports = prod_sports_array[j];

            matchQueryBuilder.fuzziness();  // 精准查询
            matchQueryBuilder.prefixLength(3); // 前缀查询的长度
            matchQueryBuilder.maxExpansions(10); // max expansion 选项，用来控制模糊查询
            QueryBuilder matchQueryBuilderProdSports = QueryBuilders.matchQuery("prod_sports", prod_sports)
                    .fuzziness();
        }

        // 精准匹配目的港
        String[] prod_eports_array = item.get(4);
        int prod_eports_size = prod_eports_array.length;
        for (int j=0; j<prod_eports_size; j++) {
            String prod_eports = prod_eports_array[j];

            matchQueryBuilder.fuzziness();  // 精准查询
            matchQueryBuilder.prefixLength(3); // 前缀查询的长度
            matchQueryBuilder.maxExpansions(10); // max expansion 选项，用来控制模糊查询
            QueryBuilder matchQueryBuilderProdEports = QueryBuilders.matchQuery("prod_eports", prod_eports)
                    .fuzziness();
        }

    }

    // 返回 产品类型
    public String getAllProductType(String transport_code) {
//        prod_transport_name???应该是产品运输方式

        // prod_keywords 的模糊匹配
        SearchRequest searchRequest = new SearchRequest("product");  // 设置搜索的 index
//        searchRequest.types("doc");  // 设置搜索的 type

        MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("prod_transport_name", transport_code);
        matchQueryBuilder.fuzziness(Fuzziness.AUTO);
        matchQueryBuilder.prefixLength(3);
        matchQueryBuilder.maxExpansions(10);

        TransportClient client = new PreBuiltTransportClient(settings)
                //获取es主机中节点的ip地址及端口号(以下是单个节点案例)
                .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.94"), 9300));
        QueryBuilder builder = QueryBuilders.matchQuery("interests", "changge");
        SearchResponse response = client.prepareSearch("lib3").setQuery(builder).get();
        SearchHits hits = response.getHits();
    }

    // 返回 为您推荐
    public String getRecommendProduct(String transport_code) {
//        prod_transport_name???应该是产品运输方式

        // prod_keywords 的模糊匹配
        SearchRequest searchRequest = new SearchRequest("product");  // 设置搜索的 index
//        searchRequest.types("doc");  // 设置搜索的 type

        MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("prod_transport_name", transport_code);
        matchQueryBuilder.fuzziness(Fuzziness.AUTO);
        matchQueryBuilder.prefixLength(3);
        matchQueryBuilder.maxExpansions(10);

        TransportClient client = new PreBuiltTransportClient(settings)
                //获取es主机中节点的ip地址及端口号(以下是单个节点案例)
                .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.94"), 9300));
        QueryBuilder builder = QueryBuilders.matchQuery("interests", "changge");
        SearchResponse response = client.prepareSearch("lib3").setQuery(builder).get();
        SearchHits hits = response.getHits();
    }

    // 返回 起始港
    public String getRecommendProduct(String transport_code) {
//        prod_transport_name???应该是产品运输方式

        // prod_keywords 的模糊匹配
        SearchRequest searchRequest = new SearchRequest("product");  // 设置搜索的 index
//        searchRequest.types("doc");  // 设置搜索的 type

        MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("prod_transport_name", transport_code);
        matchQueryBuilder.fuzziness(Fuzziness.AUTO);
        matchQueryBuilder.prefixLength(3);
        matchQueryBuilder.maxExpansions(10);

        TransportClient client = new PreBuiltTransportClient(settings)
                //获取es主机中节点的ip地址及端口号(以下是单个节点案例)
                .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.94"), 9300));
        QueryBuilder builder = QueryBuilders.matchQuery("interests", "changge");
        SearchResponse response = client.prepareSearch("lib3").setQuery(builder).get();
        SearchHits hits = response.getHits();
    }

    // 返回 目的港
    public String getRecommendProduct(String transport_code) {
//        prod_transport_name???应该是产品运输方式

        // prod_keywords 的模糊匹配
        SearchRequest searchRequest = new SearchRequest("product");  // 设置搜索的 index
//        searchRequest.types("doc");  // 设置搜索的 type

        MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("prod_transport_name", transport_code);
        matchQueryBuilder.fuzziness(Fuzziness.AUTO);
        matchQueryBuilder.prefixLength(3);
        matchQueryBuilder.maxExpansions(10);

        TransportClient client = new PreBuiltTransportClient(settings)
                //获取es主机中节点的ip地址及端口号(以下是单个节点案例)
                .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.94"), 9300));
        QueryBuilder builder = QueryBuilders.matchQuery("interests", "changge");
        SearchResponse response = client.prepareSearch("lib3").setQuery(builder).get();
        SearchHits hits = response.getHits();
    }
}
